package com.diyizu.controller.impl;

import com.diyizu.controller.ILoginController;
import com.diyizu.controller.IStudentInfoController;
import com.diyizu.domain.Student;
import com.diyizu.factory.Factory;
import com.diyizu.service.IStudentInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentInfoControllerImpl implements IStudentInfoController {
    private IStudentInfoService studentService = Factory.getStudentInfoService();
    private static final Logger LOGGER = LoggerFactory.getLogger(StudentInfoControllerImpl.class);
    Scanner sc = new Scanner(System.in);


    @Override
    public void start() {
        //死循环
        while (true) {
            //键盘录入
            System.out.println("请输入您的选择: 1.添加学生  2.删除学生  3.修改学生  4.查看学生  5.更多功能  6.退出登录");
            String choice = sc.next();
            //switch
            switch (choice) {
                case "1":
                    addStudent();
                    break;
                case "2":
                    deleteStudent();
                    break;
                case "3":
                    updateStudent();
                    break;
                case "4":
                    query();
                    break;
                case "5":
                    Extensions extensions = Factory.getExtensions();
                    extensions.start();
                    break;
                case "6":
                    System.out.println("感谢您的使用");
                    ILoginController iLoginController = Factory.getILoginController();
                    String userName = iLoginController.getUserName();
                    LOGGER.info(userName+"退出登录");
                    return;
                default:
                    System.out.println("您的输入有误,请重新输入");
            }
        }
    }

    private void query() {
        while (true) {
            System.out.println("请输入您的选择: 1.查看所有学生  2.通过手机号查找  3.通过姓名查找  4.返回上级菜单 ");
            String choice = sc.next();
            switch (choice) {
                case "1":
                    findAllStudents();
                    break;
                case "2":
                    getStudentToPhone();
                    break;
                case "3":
                    getStudentToName();
                    break;
                case "4":
                    return;
                default:
                    System.out.println("您的输入有误,请重新输入");
            }
        }
    }

    @Override
    public void addStudent() {
//键盘录入各种信息
        System.out.println("请输入学号");
        String sid = null;
        while (true) {
            sid = sc.next();
            //调用service 方法 来 判断学号是否存在
            boolean isExists = studentService.isExists(sid);
            if (isExists) {
                System.out.println("已经存在请重新输入");
            } else {
                //不存在
                break;
            }
        }
        Student student = getStudentInfo(sid);

        studentService.addStudent(student);
        System.out.println("新增成功");

    }

    @Override
    public void deleteStudent() {
        String sid = getSid();
        studentService.deleteStudent(sid);
        System.out.println("删除成功");

    }

    @Override
    public void updateStudent() {
        String sid = getSid();
        Student student = getStudentInfo(sid);
        studentService.updateStudent(sid, student);
        System.out.println("修改成功");
    }

    @Override
    public void findAllStudents() {
        ArrayList<Student> list = studentService.findAllStudents();
        if (list == null || list.size() == 0) {
            System.out.println("没有数据,请先添加");
            return;
        }
        System.out.println("学号\t姓名\t性别\t年龄\t年级\t班级\t小组\t手机号\t\t地址");
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            System.out.println(stu.getSid() + "\t" + stu.getName() + "\t" + stu.getSex() + "\t" + stu.getAge() + "\t" + stu.getGrade() + "\t" + stu.getClasses() + "\t" + stu.getGroup() + "\t" + stu.getPhone() + "\t" + stu.getAddress());
        }

    }

    @Override
    public void getStudentToPhone() {
        System.out.println("请输入要查找学生的手机号");
        String phone = sc.next();
        Student stu = studentService.getStudentToPhone(phone);
        if(stu!=null){
            System.out.println("学号\t姓名\t性别\t年龄\t年级\t班级\t小组\t手机号\t\t地址");
            System.out.println(stu.getSid() + "\t" + stu.getName() + "\t" + stu.getSex() + "\t" + stu.getAge() + "\t" + stu.getGrade() + "\t" + stu.getClasses() + "\t" + stu.getGroup() + "\t" + stu.getPhone() + "\t" + stu.getAddress());
        }else {
            System.out.println("没有找到该学生");
        }
    }

    @Override
    public void getStudentToName() {
        System.out.println("请输入要查找学生的姓名");
        String name = sc.next();
        Student stu = studentService.getStudentToName(name);
        if(stu!=null){
        System.out.println("学号\t姓名\t性别\t年龄\t年级\t班级\t小组\t手机号\t\t地址");
        System.out.println(stu.getSid() + "\t" + stu.getName() + "\t" + stu.getSex() + "\t" + stu.getAge() + "\t" + stu.getGrade() + "\t" + stu.getClasses() + "\t" + stu.getGroup() + "\t" + stu.getPhone() + "\t" + stu.getAddress());
        }else {
            System.out.println("没有找到该学生");
        }
    }

    private Student getStudentInfo(String sid) {
        System.out.println("请输入姓名");
        String name = sc.next();
        System.out.println("请输入性别");
        String sex = sc.next();
        System.out.println("请输入年龄");
        int age = sc.nextInt();
        System.out.println("请输入所在年级");
        String grade = sc.next();
        System.out.println("请输入所在班级");
        String classes = sc.next();
        System.out.println("请输入所在小组");
        String group = sc.next();
        System.out.println("请输入学生手机号");
        String phone = sc.next();
        System.out.println("请输入学生地址");
        String address = sc.next();

        Student student = new Student(sid, name, sex, age, grade, classes, group, phone, address);
        return student;
    }

    private String getSid() {
        //录入学号
        System.out.println("请输入学号");
        String sid = null;
        while (true) {
            sid = sc.next();
            //判断学号是否存在
            boolean exists = studentService.isExists(sid);
            //存在
            if (exists) {
                break;
            } else { //不存在 重新录入
                System.out.println("不存在,请重新录入");
            }
        }
        return sid;
    }
}